#!/bin/bash

if [ $# -lt 2 ]; then
  echo "usage: farmvall [options] <farmername> <dirname>" 1>&2
  echo "   runs <farmername> against everything else in the <dirname> directory" 1>&2
  exit 1
fi

dir=${@:$#:1}
farmer=${@:$(($#-1)):1}
options=${@:1:$(($#-2))}

echo "Playing ${farmer} against everything else in ${dir} ..."

# path to sim is same as to this script, with farmvall replaced by farmville
simulator=${0%farmvall}farmville

for otherfarmer in ${dir}/*; do
  if [ ! -x $otherfarmer ]; then
    # skip non-executable files
    continue
  fi
  if [ $otherfarmer == $farmer ]; then
    # skip the farmer itself
    continue
  fi
  echo "Playing $farmer vs. $otherfarmer:"
  $simulator $options $farmer $otherfarmer

  echo

  echo "Playing $otherfarmer vs. $farmer:"
  $simulator $options $otherfarmer $farmer

  echo
done
